package com.mdjukebox.model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;

import com.mdjukebox.model.vo.CancionVO;
import com.mdjukebox.model.vo.PeticionVO;
import com.util.exceptions.InstanceNotFoundException;
import com.util.exceptions.InternalErrorException;
import com.util.sql.EntityIdentifierGenerator;
import com.util.sql.GeneralOperations;

public class PeticionDAO {	
	
	public void create(Connection connection, PeticionVO vo)
	    throws InternalErrorException {
	    
	    PreparedStatement preparedStatement = null;
	
	    try {
	    	
			/* Get account identifier. */
            EntityIdentifierGenerator entityIdentifierGenerator =
                new EntityIdentifierGenerator();
            int idPeticion = 
                entityIdentifierGenerator.nextIdentifier(connection, "peticion");
	
	        /* Create "preparedStatement". */
	        String queryString = "INSERT INTO Peticion " +
	            " (id, idCancion, fchPeticion, fchReproduccion, dedicatoria) " +
	            " VALUES (?, ?, ?, ?, ?)";
	        preparedStatement = connection.prepareStatement(queryString);    
	        
	        /* Fill "preparedStatement". */
	        int i = 1;
	        preparedStatement.setInt(i++, idPeticion);
	        preparedStatement.setInt(i++, vo.getIdCancion());
	        preparedStatement.setTimestamp(i++,vo.getFchPeticion());
	        preparedStatement.setTimestamp(i++,vo.getFchReproduccion());
	        preparedStatement.setString(i++, vo.getDedicatoria());
	                    
	        /* Execute query. */
	        int insertedRows = preparedStatement.executeUpdate();
	    
	        if (insertedRows == 0) {
	            throw new SQLException("Can not add row to table" +
	                " 'Peticion'");
	        }
	        
	        vo.setIdPeticion(idPeticion);
	    
	    } catch (SQLException e) {
	        throw new InternalErrorException(e);
	    } finally {
	        GeneralOperations.closeStatement(preparedStatement);
	    }    
	    
	}	
    
    public void updateFchRep(Connection connection, Timestamp fecha, int idCancion) 
    throws InstanceNotFoundException, InternalErrorException {
    
    PreparedStatement preparedStatement = null;

    try {

        /* Create "preparedStatement". */
        String queryString = "UPDATE peticion" +
            " SET fchReproduccion = ? WHERE idCancion = ? ";
        preparedStatement = connection.prepareStatement(queryString);
        
        /* Fill "preparedStatement". */
        int i = 1;
        preparedStatement.setTimestamp(i++,fecha);
        preparedStatement.setInt(i++,idCancion);
        
        /* Execute query. */
        int updatedRows = preparedStatement.executeUpdate();

        if (updatedRows == 0) {
            throw new InstanceNotFoundException(
                idCancion, 
                CancionVO.class.getName());
        }
	        
    } catch (SQLException e) {
        throw new InternalErrorException(e);    
    } finally {
        GeneralOperations.closeStatement(preparedStatement);
    }            
    
}

}
